<?php

//微商加盟生成订单号
function build_orderid_1(){
    $yCode = array('VW', 'WX', 'XY', 'YZ', 'ZA','AB','BC','CD','DE','UV');
    $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
    return $orderSn;
} 
/**
 * @return string
 * 生成平安银行的26位的订单号
 */
function build_pinganorderids($masterId){
   $yCode = array('a', 'b', 'd', 'e', 'f','h', 'i','j', 'h');
   $orderSn = $masterId.date('Ymd',time()).$yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))). substr(time(), -2).substr(microtime(), 2,2). sprintf('%02d', rand(0, 99));
   return $orderSn;
}

//主题包生成订单号
function build_orderid_theme(){
    $yCode = array('XY', 'YZ', 'ZA','AB','BC','CD','DE','UV');
    $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
    return $orderSn;
}

//主题包生成订单号
function build_orderid_expression(){
    $yCode = array('YZ', 'ZA','AB','BC','CD','DE','FG','HI');
    $orderSn = $yCode[intval(date('Y')) - 2011] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
    return $orderSn;
}

//数组转xml
function arrayToXml($arr)
{
    $xml = "<xml>";
    foreach ($arr as $key=>$val)
    {
        if (is_numeric($val))
        {
            $xml.="<".$key.">".$val."</".$key.">";

        }
        else
            $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
    }
    $xml.="</xml>";
    return $xml;
}

/**
xml转成数组
 */
function xmlstr_to_array($xmlstr) {
    $doc = new \DOMDocument();
    $doc->loadXML($xmlstr);
    return domnode_to_array($doc->documentElement);
}
function domnode_to_array($node) {
    $output = array();
    switch ($node->nodeType) {
        case XML_CDATA_SECTION_NODE:
        case XML_TEXT_NODE:
            $output = trim($node->textContent);
            break;
        case XML_ELEMENT_NODE:
            for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
                $child = $node->childNodes->item($i);
                $v = domnode_to_array($child);
                if(isset($child->tagName)) {
                    $t = $child->tagName;
                    if(!isset($output[$t])) {
                        $output[$t] = array();
                    }
                    $output[$t][] = $v;
                }
                elseif($v) {
                    $output = (string) $v;
                }
            }
            if(is_array($output)) {
                if($node->attributes->length) {
                    $a = array();
                    foreach($node->attributes as $attrName => $attrNode) {
                        $a[$attrName] = (string) $attrNode->value;
                    }
                    $output['@attributes'] = $a;
                }
                foreach ($output as $t => $v) {
                    if(is_array($v) && count($v)==1 && $t!='@attributes') {
                        $output[$t] = $v[0];
                    }
                }
            }
            break;
    }
    return $output;
}
